package com.baosight.risk.web.ridc;


import com.baosight.risk.api.ridc.RidcRiskService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.service.ridc.entity.*;
import com.baosight.risk.service.ridc.vo.RidcRiskAdvanceVO;
import com.baosight.risk.service.ridc.vo.RidcRiskDescVO;
import com.baosight.risk.service.ridc.vo.RidcRiskEvaluateVO;
import com.baosight.risk.service.ridc.vo.RidcRiskVO;
import com.baosight.risk.service.riim.entity.RiimRiskImport;
import com.baosight.risk.service.riim.vo.RiimRiskImportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author houmingcheng
 * @Date 2019.7.13
 */

@Slf4j
@RestController
@RequestMapping("/ridc/risk")
@Api(value = "风险档案", description = "风险档案", produces = MediaType.APPLICATION_JSON)
public class RidcRiskController extends BaseController {


    @Autowired
    private RidcRiskService ridcRiskService;

    @RequestMapping(value = "/selectList", method = RequestMethod.GET)
    @ApiOperation("查询风险档案列表")
    public AjaxResult selectList(RidcRisk ridcRisk) {
        log.info("查询风险档案列表：" + ridcRisk.toString());
        startPage();
        return getResultPage(ridcRiskService.selectList(ridcRisk));
    }

    @RequestMapping(value = "/selectListByIds", method = RequestMethod.POST)
    @ApiOperation("查询风险档案列表")
    public AjaxResult selectList(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return error("系统出错啦!没有数据。");
        }
        return success(ridcRiskService.selectListByIds(ids));
    }

    @PostMapping("/saveRidcRisk")
    @Log(title = "保存风险档案详细信息", businessType = BusinessType.INSERT)
    @ApiOperation("保存风险档案详细信息")
    public AjaxResult saveRidcRisk(@RequestBody RidcRiskVO ridcRiskVO) {
        ridcRiskService.saveRidcRisk(ridcRiskVO);
        log.info("保存成功");
        return success("保存成功");
    }

    @GetMapping("/getRidcRiskDetailById")
    @ApiOperation("获取风险档案详细信息")
    public AjaxResult getRidcRiskDetail(@RequestParam("id") String id) {
        return success("获取成功", ridcRiskService.getRidcRiskDetail(id));
    }

    @PostMapping("/updateStatus")
    @Log(title = "风险档案状态修改", businessType = BusinessType.UPDATE)
    @ApiOperation("风险档案状态修改")
    public AjaxResult updateStatus(@RequestBody MultipleUpdateStatus multipleUpdateStatus) {
        if (CollectionUtils.isEmpty(multipleUpdateStatus.getIds())) {
            log.error("请至少选择一条数据");
            return error("请至少选择一条数据！");
        }
        return toAjax(ridcRiskService.updateStatus(multipleUpdateStatus));
    }

    @PostMapping("/doSubmitRidcRisk")
    @Log(title = "风险档案提交")
    @ApiOperation("风险档案提交")
    public AjaxResult doSubmitRidcRisk(@RequestBody RidcRiskVO ridcRiskVO) {
        ridcRiskService.saveRidcRisk(ridcRiskVO);
        RidcRisk ridcRisk = ridcRiskVO.getRidcRisk();
        //接待办
        MultipleUpdateStatus multipleUpdateStatus = new MultipleUpdateStatus();
        multipleUpdateStatus.setIds(Arrays.asList(ridcRiskVO.getRidcRisk().getId()));
        multipleUpdateStatus.setStatus("B01");
        multipleUpdateStatus.setStatusName("提交");
        ridcRiskService.updateStatus(multipleUpdateStatus);
        return success("提交成功");
    }

    @PostMapping("/saveRidcRiskDesc")
    @Log(title = "保存风险档案描述")
    @ApiOperation("保存风险档案描述")
    public AjaxResult saveRidcRiskDesc(@RequestBody RidcRiskDesc ridcRiskDesc) {
        ridcRiskService.saveRidcRiskDesc(ridcRiskDesc);
        return success("保存风险描述成功");
    }

    @PostMapping("/saveRidcRiskResult")
    @Log(title = "保存风险档档案评估结论")
    @ApiOperation("保存风险档案评估结论")
    public AjaxResult saveRidcRiskResult(@RequestBody RidcRiskEvaluateResult ridcRiskEvaluateResult) {
        ridcRiskService.saveRidcRiskResult(ridcRiskEvaluateResult);
        return success("保存风险成功");
    }

    @PostMapping("/saveRidcRiskEvaluate")
    @Log(title = "保存风险评估")
    @ApiOperation("保存风险评估")
    public AjaxResult saveRidcRiskEvaluate(@RequestBody RidcRiskEvaluate ridcRiskEvaluate) {
        ridcRiskService.saveRidcRiskEvaluate(ridcRiskEvaluate);
        return success("保存风险评论成功");
    }

    @PostMapping("/saveRidcRiskAdvance")
    @Log(title = "保存监督与改进")
    @ApiOperation("保存监督与改进")
    public AjaxResult saveRidcRiskAdvance(@RequestBody RidcRiskAdvance ridcRiskAdvance) {
        ridcRiskService.saveRidcRiskAdvance(ridcRiskAdvance);
        return success("保存风险风险监督与改进成功");
    }

    @PostMapping(value = "/deleteRidcRiskDescById")
    @Log(title = "删除风险描述")
    @ApiOperation("删除风险描述")
    public AjaxResult deleteRidcRiskDescById(@RequestBody MultipleUpdateStatus multipleUpdateStatus) {
        List<String> ids = multipleUpdateStatus.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            return error("请至少选择一条记录！");
        }
        Integer integer = ridcRiskService.deleteRidcRiskDescByIds(ids);
        return toAjax(integer);
    }

    @PostMapping("/deleteRidcRiskEvaById")
    @Log(title = "删除风险评估")
    @ApiOperation("删除风险评估")
    public AjaxResult deleteRidcRiskEvaById(@RequestBody MultipleUpdateStatus multipleUpdateStatus) {
        List<String> ids = multipleUpdateStatus.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            return error("请至少选择一条记录！");
        }

        Integer integer = ridcRiskService.deleteRidcRiskEvaByIds(ids);
        return toAjax(integer);

    }

    @PostMapping("/deleteRidcRiskAdById")
    @Log(title = "删除风险评估")
    @ApiOperation("删除风险评估")
    public AjaxResult deleteRidcRiskAdById(@RequestBody MultipleUpdateStatus multipleUpdateStatus) {
        List<String> ids = multipleUpdateStatus.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            return error("请至少选择一条记录！");
        }
        Integer integer = ridcRiskService.deleteRidcRiskAdByIds(ids);
        return toAjax(integer);

    }

    @PostMapping("/selectListByDept")
    @Log(title = "根据本单位查询风险档案")
    @ApiOperation("根据本单位查询风险档案")
    public AjaxResult selectListByDept(@RequestBody RiimRiskImportVO importVO) {
        startPage();
        return getResultPage(ridcRiskService.selectListByDept(importVO));
    }
    @PostMapping("/updateRowOrderDesc")
    @Log(title = "更新风险描述顺序")
    @ApiOperation("更新风险描述顺序")
    public AjaxResult updateRowOrderDesc(@RequestBody RidcRiskDescVO vo) {
        ridcRiskService.updateRowOrderDesc(vo);
        return  success();
    }
    @PostMapping("/updateRowOrderAdvance")
    @Log(title = "更新风险描述顺序")
    @ApiOperation("更新风险描述顺序")
    public AjaxResult updateRowOrderAdvance(@RequestBody RidcRiskAdvanceVO vo) {
        ridcRiskService.updateRowOrderAdvance(vo);
        return  success();
    }
    @PostMapping("/updateRowOrderEvaluate")
    @Log(title = "更新风险描述顺序")
    @ApiOperation("更新风险描述顺序")
    public AjaxResult updateRowOrderEvaluate(@RequestBody RidcRiskEvaluateVO vo) {
        ridcRiskService.updateRowOrderEvaluate(vo);
        return  success();
    }

    @GetMapping("/pushArchives")
    @ApiOperation("推送风险档案")
    public AjaxResult pushArchives(String ridcRiskId) {
        Map returnMap = ridcRiskService.pushArchives(ridcRiskId);
        if ("1".equals(returnMap.get("status"))) {
            return success();
        }
        return error(returnMap.get("msg").toString());
    }
}